window.addEventListener('load', function () {
    var focus = document.querySelector('.focus');
    var arrowL = document.querySelector('.arrow-l');
    var arrowR = document.querySelector('.arrow-r');
    focus.addEventListener('mouseenter', function () {
        arrowL.style.display = 'block';
        arrowR.style.display = 'block';
    });
    focus.addEventListener('mouseleave', function () {
        arrowL.style.display = 'none';
        arrowR.style.display = 'none';
    });
    var focusUl = focus.querySelector('ul');
    var focusLi = focusUl.querySelectorAll('li');
    var circle = document.querySelector('.circle');
    for (var i = 0; i <= focusLi.length - 1; i++) {
        var circleLi = document.createElement('li');
        circle.appendChild(circleLi);
        circleLi.setAttribute('index', i);
        circleLi.addEventListener('click', function () {
            for (var j = 0; j <= focusLi.length - 1; j++) {
                circle.children[j].className = '';
            }
            this.className = 'currentCirlce';
            var index = this.getAttribute('index');//自定义index属性
            num = cir = index;
            var focusoffset = focus.offsetWidth;
            animate(focusUl, -focusoffset * index);//调用了另一个js文件中的函数
        });
    }
    /*每个小圆圈提出来做
    var cirLi = circle.querySelectorAll('li');
    console.log(cirLi.length - 1);
    for (var i = 0; i <= cirLi.length - 1; i++) {
        cirLi[i].addEventListener('click', function () {
            for (var j = 0; j <= cirLi.length - 1; j++) {
                cirLi[j].className = '';
            }
            this.className = 'currentCirlce';
        });
    */
    circle.children[0].className = 'currentCirlce';
    var num = 0;//方向键点击
    var cir = 0;//小圆点点击
    var flag = true;//节流阀
    var firstPicture = focusUl.children[0].cloneNode(true);
    focusUl.appendChild(firstPicture);//复制第一张图片
    arrowR.addEventListener('click', function () {
        if (flag) {
            flag = false;
            if (num > focusLi.length - 1) {
                focusUl.style.left = 0;
                num = 0;
            }
            num++;
            var focusoffset = focus.offsetWidth;
            animate(focusUl, -focusoffset * num, function () {
                flag = true;
            });
            cir++;
            if (cir == focusLi.length) {
                cir = 0;
            }
            for (var i = 0; i <= focusLi.length - 1; i++) {//排他
                circle.children[i].className = '';
            }
            if (cir <= focusLi.length - 1) {
                circle.children[cir].className = 'currentCirlce';
            }
        }
    });
    arrowL.addEventListener('click', function () {
        if (flag) {
            flag = false;
            if (num == 0) {
                focusUl.style.left = -(focusLi.length - 1) * focus.offsetWidth + 'px';
                num = cir = focusLi.length;
            }
            num--;
            cir--;
            var focusoffset = focus.offsetWidth;
            if (num >= 0) {
                animate(focusUl, -focusoffset * num, function () {
                    flag = true;
                });
            }
            for (var i = 0; i <= focusLi.length - 1; i++) {
                circle.children[i].className = '';
            }
            if (cir >= 0) {
                circle.children[cir].className = 'currentCirlce';
            }
        }
    });
    //自动播放,定时器
    focus.addEventListener('mouseleave', function () {
        var timer = setInterval(function () {
            arrowR.click();//手动调用事件
        }, 2000);
        focus.addEventListener('mouseenter', function () {
            clearInterval(timer);
        });
    });

});
